﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using mini_crm_Entity;
using System.Data.SqlClient;

namespace mini_crm_Bo
{
    public class CustomerManager
    {
        AccessHelperSQL accessHelper = AccessHelperSQL.GetInstance();

        List<T_Customer> ConvertTableToObjs(DataTable dt)
        {
            List<T_Customer> returnlist = new List<T_Customer>();
            foreach (DataRow dr in dt.Rows)
            {
                returnlist.Add(new T_Customer
                {
                    ID = Convert.ToInt32(dr["ID"]),
                    CustomerID = dr["CustomerID"] == DBNull.Value ? 0 : Convert.ToInt32(dr["CustomerID"]),
                    Email = dr["Email"] == DBNull.Value ? "" : dr["Email"].ToString(),
                    Telephone = dr["Telephone"] == DBNull.Value ? "" : dr["Telephone"].ToString(),
                    Source = dr["Source"] == DBNull.Value ? "" : dr["Source"].ToString(),
                    Mobile = dr["Mobile"] == DBNull.Value ? "" : dr["Mobile"].ToString(),
                    FirstName = dr["FirstName"] == DBNull.Value ? "" : dr["FirstName"].ToString(),
                    LastName = dr["LastName"] == DBNull.Value ? "" : dr["LastName"].ToString(),
                    Birthday = dr["Birthday"] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr["Birthday"].ToString()),
                    LastLoginTime = dr["LastLoginTime"] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr["LastLoginTime"].ToString())
                });
            }
            return returnlist;
        }

        public List<T_Customer> GetObjListByCondition()
        {
            var sql = string.Format(@"select * from T_Customer");
            var dt = accessHelper.ExcuteDataTable(sql);
            return ConvertTableToObjs(dt);
        }
        
        public bool ToImportCustomer(DataSet ds)
        {
            
            if (ds.Tables.Count < 1)
                return false;
            DataTable dt = ds.Tables[0];
            dt.Columns.Add(new DataColumn() { ColumnName = "Source", DefaultValue = "导入" });

            try
            {
                using (SqlConnection conn = new SqlConnection((new AccessHelperSQL()).strConnection))
                {
                    SqlBulkCopy bcp = new SqlBulkCopy(conn);
                    bcp.DestinationTableName = "T_Customer";
                    //bcp.BatchSize = 100;//这是批尺寸可以调整

                    bcp.ColumnMappings.Add("M", "Mobile");
                    bcp.ColumnMappings.Add("Source", "Source");
                    bcp.ColumnMappings.Add("LT", "LastLoginTime");

                    //bcp.SqlRowsCopied += new SqlRowsCopiedEventHandler(bulkCopy_SqlRowsCopied);
                    //TimeSpan startTime = System.Diagnostics.Process.GetCurrentProcess().UserProcessorTime;
                    conn.Open();
                    bcp.WriteToServer(dt);


                    //TimeSpan duration = System.Diagnostics.Process.GetCurrentProcess().UserProcessorTime.Subtract(startTime);
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }

        }
         

    }
}
